/*
 * _______________#########_______________________ 
 * ______________############_____________________ 
 * ______________#############____________________ 
 * _____________##__###########___________________ 
 * ____________###__######_#####__________________ 
 * ____________###_#######___####_________________ 
 * ___________###__##########_####________________ 
 * __________####__###########_####_______________ 
 * ________#####___###########__#####_____________ 
 * _______######___###_########___#####___________ 
 * _______#####___###___########___######_________ 
 * ______######___###__###########___######_______ 
 * _____######___####_##############__######______ 
 * ____#######__#####################_#######_____ 
 * ____#######__##############################____ 
 * ___#######__######_#################_#######___ 
 * ___#######__######_######_#########___######___ 
 * ___#######____##__######___######_____######___ 
 * ___#######________######____#####_____#####____ 
 * ____######________#####_____#####_____####_____ 
 * _____#####________####______#####_____###______ 
 * ______#####______;###________###______#________ 
 * ________##_______####________####______________ 
 */

/*
 * @Author: 火红色祥云 809880192@qq.com
 * @Date: 2022-06-25 15:56:04
 * @LastEditors: 火红色祥云 809880192@qq.com
 * @LastEditTime: 2022-10-06 20:58:01
 * @FilePath: /imx6ull-app/14_platform/14_leddriver.c
 * @Description: 设备驱动
 */

#include "linux/init.h"
#include "linux/module.h"

#include "linux/platform_device.h"
#include "linux/device.h"
#include "linux/cdev.h"
#include "linux/fs.h"

#include "linux/uaccess.h"
#include "asm/io.h"
#include "asm/mach/map.h"
#include "linux/types.h"
#include "linux/ide.h"

#include "linux/gpio.h"
#include "linux/of_gpio.h"

#include "linux/kernel.h"
#include "linux/fcntl.h"
#include <linux/delay.h>

#define NAME    "leddriver"

static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO03;
static void __iomem *SW_PAD_GPIO1_IO03;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;

static struct ST_DRIVE{
    dev_t devid;
    int major;
    int minor;
    struct cdev st_cdev;
    struct device *st_device;
    struct class *st_class;
}st_driver;

void led0_switch(u8 sta)
{
    u32 val = 0;
    if (sta == 1)
    {
        val = readl(GPIO1_DR);
        val &= ~(1<<3);
        writel(val,GPIO1_DR);
    }
    else
    {
        val = readl(GPIO1_DR);
        val |= (1<<3);
        writel(val,GPIO1_DR);
    }
}

/**************************************************
* Copyright (c) 2022-2030, Lux.Co.Ltd
* filename: leddriver_open
* function: 
* param   : 
* return  : 
* auther  : Lux
* Date    : 2022-10-6
* Version : V1.0
**************************************************/
int leddriver_open(struct inode *nd, struct file *fp)
{
    fp->private_data = &st_driver;
    return 0;
}

/**************************************************
* Copyright (c) 2022-2030, Lux.Co.Ltd
* filename: leddriver_write
* function: 
* param   : 
* return  : 
* auther  : Lux
* Date    : 2022-10-6
* Version : V1.0
**************************************************/
ssize_t leddriver_write(struct file *fp, const char __user *buf, size_t len, loff_t *offset)
{
    int retval;

    unsigned char databuf[1];
    unsigned char ledsta;

    retval = copy_from_user(databuf,buf,len);
    if (retval < 0)
    {
        return -EFAULT;
    }
    
    ledsta = databuf[0];

    if (ledsta == 1)
    {
        led0_switch(1);
    }
    else
    {
        led0_switch(0);
    }

    return 0;
}

/**************************************************
* Copyright (c) 2022-2030, Lux.Co.Ltd
* filename: leddriver_release
* function: 
* param   : 
* return  : 
* auther  : Lux
* Date    : 2022-10-6
* Version : V1.0
**************************************************/
int leddriver_release(struct inode *nd, struct file *fp)
{
    return 0;
}

static struct file_operations fop_leddriver = {
    .owner = THIS_MODULE,
    .open = leddriver_open,
    .write = leddriver_write,
    .release = leddriver_release,
};

/**************************************************
* Copyright (c) 2022-2030, Lux.Co.Ltd
* filename: led_probe
* function: 当设备和设备驱动匹配成功后执行probe函数
* param   : 
* return  : 
* auther  : Lux
* Date    : 2022-10-6
* Version : V1.0
**************************************************/
static int led_probe(struct platform_device *dev)
{
    int i=0;
    struct resource *ledsource[5];
    int ressize[5];

    u32 val;

    for (i = 0; i < 5; i++)
    {
        ledsource[i] = platform_get_resource(dev,IORESOURCE_MEM,i);
        ressize[i] = resource_size(ledsource[i]);
    }
    
    IMX6U_CCM_CCGR1 = ioremap(ledsource[0]->start,ressize[0]);
    SW_MUX_GPIO1_IO03 = ioremap(ledsource[1]->start,ressize[1]);
    SW_PAD_GPIO1_IO03 = ioremap(ledsource[2]->start,ressize[2]);
    GPIO1_DR = ioremap(ledsource[3]->start,ressize[3]);
    GPIO1_GDIR = ioremap(ledsource[4]->start,ressize[4]);

    val = readl(IMX6U_CCM_CCGR1);
    val &= ~(3<<26);
    val |= (3<<26);
    writel(val,IMX6U_CCM_CCGR1);

    writel(5,SW_MUX_GPIO1_IO03);
    writel(0x10B0,SW_PAD_GPIO1_IO03);

    val = readl(GPIO1_GDIR);
    val &= ~(1<<3);
    val |= (1<<3);
    writel(val,GPIO1_GDIR);

    val = readl(GPIO1_DR);
    val |= (1<<3);
    writel(val,GPIO1_DR);

    //int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);
    alloc_chrdev_region(&st_driver.devid,0,1,NAME);
    st_driver.major = MAJOR(st_driver.devid);
    st_driver.minor = MINOR(st_driver.devid);

    st_driver.st_cdev.owner = THIS_MODULE;
    cdev_init(&st_driver.st_cdev,&fop_leddriver);
    cdev_add(&st_driver.st_cdev,st_driver.devid,1);

    st_driver.st_class = class_create(THIS_MODULE,NAME);
    device_create(st_driver.st_class,NULL,st_driver.devid,NULL,NAME);

    return 0;
}
static int led_remove(struct platform_device *dev)
{

    iounmap(IMX6U_CCM_CCGR1);
    iounmap(SW_MUX_GPIO1_IO03);
    iounmap(SW_PAD_GPIO1_IO03);
    iounmap(GPIO1_DR);
    iounmap(GPIO1_GDIR);

    cdev_del(&st_driver.st_cdev);
    unregister_chrdev_region(st_driver.devid,1);
    device_destroy(st_driver.st_class,st_driver.devid);
    class_destroy(st_driver.st_class);
    return 0;
}
/*
struct platform_driver {
	int (*probe)(struct platform_device *);
	int (*remove)(struct platform_device *);
	void (*shutdown)(struct platform_device *);
	int (*suspend)(struct platform_device *, pm_message_t state);
	int (*resume)(struct platform_device *);
	struct device_driver driver;
	const struct platform_device_id *id_table;
	bool prevent_deferred_probe;
};
*/
static struct platform_driver led_driver = {
    .driver = {
        .name = "lux-led",
    },
    .probe = led_probe,
    .remove = led_remove,
};

/*
int __platform_driver_register(struct platform_driver *,
					struct module *);
*/
/**************************************************
* Copyright (c) 2022-2030, Lux.Co.Ltd
* filename: leddriver_init
* function: 设备驱动注册
* param   : 
* return  : 
* auther  : Lux
* Date    : 2022-10-6
* Version : V1.0
**************************************************/
static int __init leddriver_init(void)
{
    return platform_driver_register(&led_driver);
}
/**************************************************
* Copyright (c) 2022-2030, Lux.Co.Ltd
* filename: leddriver_exit
* function: 设备驱动注销
* param   : 
* return  : 
* auther  : Lux
* Date    : 2022-10-6
* Version : V1.0
**************************************************/
static void __exit leddriver_exit(void)
{
    platform_driver_unregister(&led_driver);
}

module_init(leddriver_init);
module_exit(leddriver_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Lux");